<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Socket</title>
        <script type="text/javascript" src="/js/jquery.js"></script>
        <script type="text/javascript" src="/js/rpc.js"></script>
        <script type="text/javascript" src="/js/utils.js"></script>
        <script type="text/javascript" src="/js/ws.js"></script>
        <script type="text/javascript">

          var json = {"protocol":2,"msgId":11977,"reqId":7832,"sessionId":162,"len":0,"version":"0.0.1","type":1,"flag":0,
            "payload":"{serviceName:'cn.jmicro.example.api.ITestRpcService',method:'getPerson'," +
            "args: [{username:'Client person Name',id:1234}],namespace:'testrpc',"+
            "version:'0.0.1',impl:'cn.jmicro.example.provider.TestRpcServiceImpl',reqId:7832,isMonitorEnable:false,params:{} }"};

          $(document).ready(function(){
            jmicro.socket.init();
            //jmicro.socket.emit(json)
            /*jmicro.socket.registListener(2,function(msg){
                console.log(msg);
            });*/
          });

          function sendMsg(msg){
            jmicro.socket.emit(json)
          }

          function getId(){
            jmicro.rpc.getId(jmicro.Constants.MessageCls)
            .then(function(id){
                console.log(id);
            }).catch(function(err){
              console.log(err);
            });
          }

          function sayHello(){
            var req = new jmicro.rpc.ApiRequest();
            req.serviceName='cn.jmicro.example.api.ITestRpcService';
            req.method = 'hello';
            req.namespace = 'testrpc';
            req.version = '0.0.1';
            req.args = ['hello api gateway'];

            jmicro.rpc.callRpc(req)
              .then(function(rst){
                console.log(rst);
              }).catch(function(err){
              console.log(err);
            });
          }

          function sayHello1(){
            var req = {};
            req.serviceName='cn.jmicro.example.api.ITestRpcService';
            req.method = 'hello';
            req.namespace = 'testrpc';
            req.version = '0.0.1';
            req.args = ['hello api gateway'];

            jmicro.rpc.callRpc(req)
              .then(function(rst){
                console.log(rst);
              }).catch(function(err){
              console.log(err);
            });
          }

          function getPerson(){
            var req = {};
            req.serviceName='cn.jmicro.example.api.ITestRpcService';
            req.namespace = 'testrpc';
            req.version = '0.0.1';
            req.method = 'getPerson';
            req.args = [{username:'Hello JS RPC',id:222}];

            jmicro.rpc.callRpc(req)
              .then(function(rst){
                console.log(rst);
              }).catch(function(err){
              console.log(err);
            });
          }

          function getPerson1(){
            jmicro.rpc.callRpc('cn.jmicro.example.api.ITestRpcService','testrpc','0.0.1','getPerson',[{username:'Hello JS RPC',id:222}])
              .then(function(rst){
                console.log(rst);
              }).catch(function(err){
              console.log(err);
            });
          }

          function testStream(){
            jmicro.rpc.callRpc('cn.jmicro.example.api.ITestRpcService','testrpc','0.0.1','subscrite',['stream test'],function(rst){
              console.log(rst);
            },true)
              .then(function(rst){
                console.log(rst);
              }).catch(function(err){
                console.log(err);
            });
          }

        </script>
    </head>
<body>
    <form onsubmit="return false">
        <textarea style="width: 300px; height: 200px;" name="message"></textarea>
        <input type="button" onclick="sendMsg(this.form.message.value)" value="发送"><br>
        <input type="button" onclick="getId()" value="GetId"><br>
        <input type="button" onclick="sayHello()" value="sayHello"><br>
        <input type="button" onclick="sayHello1()" value="sayHello1"><br>
        <input type="button" onclick="getPerson()" value="getPerson"><br>
        <input type="button" onclick="getPerson1()" value="getPerson1"><br>
        <input type="button" onclick="testStream()" value="testStream"><br>

        <h3>信息</h3>
        <textarea style="width: 300px; height: 200px;" id="respMessage"></textarea>
        <input type="button" value="清空" onclick="javascript:document.getElementById('respMessage').value = ''">
    </form>
</body>
</html>
